﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ScarecrowLibrary;

namespace ScarecrowLibrary.Network
{
    [Serializable]
    public class scNetworkProfile
    {
        #region Properties
        public string Name
        {
            get;
            set;
        }
        public scAdapterProfile this[string key]
        {
            get { return this.profiles[key]; }
        }
        public string ImagePath
        {
            get;
            set;
        }
        #endregion

        #region Fields
        Dictionary<string, scAdapterProfile> profiles = new Dictionary<string, scAdapterProfile>();

        #endregion

        #region Methods
        public scNetworkProfile()
        {
            this.Name = "";
            this.ImagePath = "";
        }
        public scNetworkProfile(string name)
        {
            this.Name = name;
            this.ImagePath = "";
        }
        public scAdapterProfile[] GetProfiles()
        {
            return this.profiles.Values.ToArray();
        }
        public bool AddAdapterProfile(scAdapterProfile profile)
        {
            if (this.profiles.ContainsKey(profile.NetworkAdapter))
                return false;
            this.profiles.Add(profile.NetworkAdapter, profile);
            this.profiles = this.profiles.OrderBy((pair) => (pair.Key)).ToDictionary( pair => pair.Key, pair => pair.Value);
            return true;
        }
        public bool RemoveAdapterProfile(string profileName)
        {
            if (!this.profiles.ContainsKey(profileName))
                return false;
            this.profiles.Remove(profileName);
            return true;
        }
        #endregion

        #region Ovverride
        public override int GetHashCode()
        {
            Int32 hashCode =  (this.Name ?? "").GetHashCode() ^ (this.ImagePath ?? "").GetHashCode();
            foreach (string key in this.profiles.Keys)
            {
                hashCode ^= key.GetHashCode() ^ this.profiles[key].GetHashCode();
            }
            return hashCode;
        }
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }
        public bool Equals(scNetworkProfile na)
        {
            if ((object)na == null)
                return false;

            return this.GetHashCode() == na.GetHashCode();
        }
        public static bool operator ==(scNetworkProfile np1, scNetworkProfile np2)
        {
            if (object.ReferenceEquals(np1, np2))
                return true;

            if ((object)np1 == null || (object)np2 == null)
                return false;

            return np1.GetHashCode() == np2.GetHashCode();
        }
        public static bool operator !=(scNetworkProfile np1, scNetworkProfile np2)
        {
            return !(np1 == np2);
        }
        #endregion

        #region Static Methods
        public static scNetworkProfile GetCurrentProfile()
        {
            scNetworkProfile np = new scNetworkProfile();
            np.Name = "Current";
            np.ImagePath = "";
            scNetworkAdapter[] nics = scNetworkAdapter.GetAllInterfaces();
            foreach (scNetworkAdapter nic in nics)
            {
                np.AddAdapterProfile(nic.ExportToProfile());
            }
            return np;
        }
        #endregion
    }
}
